package org.dizem.groupon.action;

import com.opensymphony.xwork2.ActionContext;
import org.apache.log4j.Logger;
import org.dizem.groupon.model.domain.OrderFormEntity;
import org.dizem.groupon.service.OrderFormService;
import org.dizem.util.Pager;

import java.util.List;

/**
 * User: dizem
 * Time: 11-7-21 下午2:53
 */
public class OrderFormAction extends BaseAction {
	private Logger log = Logger.getLogger(OrderFormAction.class);

	private OrderFormService service;
	private String username;
	private OrderFormEntity orderForm;
	private List<OrderFormEntity> orderForms;
	private int id;
	private int itemId;
	private int number;
	private float price;
	private boolean isWaiting;

	public boolean isWaiting() {
		return isWaiting;
	}

	public void setWaiting(boolean waiting) {
		isWaiting = waiting;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public List<OrderFormEntity> getOrderForms() {
		return orderForms;
	}

	public void setOrderForms(List<OrderFormEntity> orderForms) {
		this.orderForms = orderForms;
	}

	public int getItemId() {
		return itemId;
	}

	public void setItemId(String itemId) {
		this.itemId = Integer.parseInt(itemId);

	}

	public int getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = Integer.parseInt(number);
	}

	public float getPrice() {
		return price;
	}

	public void setPrice(String price) {
		this.price = Float.parseFloat(price);
	}

	public OrderFormService getService() {
		return service;
	}

	public void setService(OrderFormService service) {
		this.service = service;
	}

	public OrderFormEntity getOrderForm() {
		return orderForm;
	}

	public void setOrderForm(OrderFormEntity orderForm) {
		this.orderForm = orderForm;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String add() {
		service.save(orderForm, username, itemId, number, price);
		return SUCCESS;
	}

	public String listWaitingList() {
		if (this.getRecordCount() < 0) {
			setRecordCount(service.getWaitingCount());
		}
		if (getPage() == null) {
			setPage(1);
		}
		Pager p = new Pager(getPage(), getRecordCount(), getPageSize());
		setPageCount(p.getPageCount());
		setPageUrl("listWaitingOrder");
		setPage(p.getPage());
		isWaiting = true;
		ActionContext.getContext().getSession().put("isWaiting", "1");
		orderForms = service.getWaitingList(p.getStartPage(), this.getPageSize());

		return SUCCESS;
	}

	public String listSuccessList() {
		if (this.getRecordCount() < 0) {
			setRecordCount(service.getSuccessCount());
		}
		if (getPage() == null) {
			setPage(1);
		}
		Pager p = new Pager(getPage(), getRecordCount(), getPageSize());
		setPageCount(p.getPageCount());
		setPageUrl("listSuccessOrder");
		setPage(p.getPage());
		isWaiting = false;
		ActionContext.getContext().getSession().put("isWaiting", "0");
		orderForms = service.getSuccessList(p.getStartPage(), this.getPageSize());

		return SUCCESS;
	}

	public OrderFormEntity getModel() {
		return orderForm;
	}

	public String acceptOrder() {
		service.acceptOrder(id);
		return SUCCESS;
	}
}
